<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Nginx 入门篇 | 乘风破浪</title><meta name="keywords" content="nginx,centos"><meta name="author" content="PAN"><meta name="copyright" content="PAN"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="特点 支持高并发 内存消耗少 开源可商用  一、安装Mac 下安装1、安装 brew 命令 1ruby -e &quot;$(curl -fsSL https:&#x2F;&#x2F;raw.githubusercontent.com&#x2F;Homebrew&#x2F;install&#x2F;master&#x2F;install)&quot;  2、安装 Nginx 1brew insta">
<meta property="og:type" content="article">
<meta property="og:title" content="Nginx 入门篇">
<meta property="og:url" content="https://geekweb.site/2019/01/22/nginxbase/index.html">
<meta property="og:site_name" content="乘风破浪">
<meta property="og:description" content="特点 支持高并发 内存消耗少 开源可商用  一、安装Mac 下安装1、安装 brew 命令 1ruby -e &quot;$(curl -fsSL https:&#x2F;&#x2F;raw.githubusercontent.com&#x2F;Homebrew&#x2F;install&#x2F;master&#x2F;install)&quot;  2、安装 Nginx 1brew insta">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2021/03/26/dhQMaZic2KpjLsG.png">
<meta property="article:published_time" content="2019-01-22T11:09:40.000Z">
<meta property="article:modified_time" content="2020-09-25T06:50:58.238Z">
<meta property="article:author" content="PAN">
<meta property="article:tag" content="nginx">
<meta property="article:tag" content="centos">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/03/26/dhQMaZic2KpjLsG.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://geekweb.site/2019/01/22/nginxbase/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":false,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-09-25 14:50:58'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><meta name="generator" content="Hexo 5.2.0"><link rel="alternate" href="/atom.xml" title="乘风破浪" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="https://i.loli.net/2020/09/25/RVKnjUAof6kryGi.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">13</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">25</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://i.loli.net/2021/03/26/dhQMaZic2KpjLsG.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">乘风破浪</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Nginx 入门篇</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-01-22T11:09:40.000Z" title="发表于 2019-01-22 19:09:40">2019-01-22</time><span class="post-meta-separator"></span></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%BC%96%E7%A8%8B/">编程</a></span></div><div class="meta-secondline"></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="特点"><a href="#特点" class="headerlink" title="特点"></a>特点</h2><ol>
<li>支持高并发</li>
<li>内存消耗少</li>
<li>开源可商用</li>
</ol>
<h2 id="一、安装"><a href="#一、安装" class="headerlink" title="一、安装"></a>一、安装</h2><p><strong>Mac 下安装</strong><br>1、安装 brew 命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ruby -e &quot;$(curl -fsSL https:&#x2F;&#x2F;raw.githubusercontent.com&#x2F;Homebrew&#x2F;install&#x2F;master&#x2F;install)&quot;</span><br></pre></td></tr></table></figure>

<p>2、安装 Nginx</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">brew install nginx</span><br></pre></td></tr></table></figure>

<p>3、启动 Nginx</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nginx</span><br></pre></td></tr></table></figure>

<p>如果没有报错的话打开 <a target="_blank" rel="noopener" href="http://localhost:8080/">http://localhost:8080</a> 就可以看到效果了。</p>
<p>配置文件所在目录 /usr/local/etc/nginx/nginx.conf</p>
<p>安装文件所在目录 /usr/local/Cellar/nginx</p>
<p><strong>Linux 下安装（本文后面的内容也主要是在 Centos 环境中演示）</strong><br>先安装一些服务器要用到的东西</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">yum -y install gcc gcc-c++ autoconf pcre-devel make automake</span><br><span class="line">yum -y install wget httpd-tools vim</span><br></pre></td></tr></table></figure>
<p>查看 yum 是否存在</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum list | grep nginx</span><br></pre></td></tr></table></figure>

<p>如果存在会如以下图示</p>
<p><img src="http://wx3.sinaimg.cn/large/889d1a94gy1fzf7pigjyjj20mr08fabf.jpg" alt="image"></p>
<p>和官网的对比一下发现不是最新的，所以要增加 nginx 的 yum 源，输入以下命令新建</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/yum.repos.d/nginx.repo</span><br></pre></td></tr></table></figure>

<p>并把以下内容粘贴进去</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[nginx]</span><br><span class="line">name=nginx repo</span><br><span class="line">baseurl=http://nginx.org/packages/centos/7/<span class="variable">$basearch</span>/</span><br><span class="line">gpgcheck=0</span><br><span class="line">enabled=1</span><br></pre></td></tr></table></figure>

<p>因为我的是 centos 系统，版本是7.2，所以链接里面是 /centos/7，现在再运行一下检查命令就可以查看到最新 nginx 版本了</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum list | grep nginx</span><br></pre></td></tr></table></figure>

<p>接着就是安装了</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install nginx</span><br></pre></td></tr></table></figure>

<p>安装完成后输入 <font color="#f60">nginx -v</font> 查看版本号，如果显示版本就表示安装成功了</p>
<p><img src="http://ws4.sinaimg.cn/large/889d1a94gy1fzf88dbsxnj20ng0e140b.jpg" alt="image"></p>
<h2 id="二、Nginx-配置文件解析"><a href="#二、Nginx-配置文件解析" class="headerlink" title="二、Nginx 配置文件解析"></a>二、Nginx 配置文件解析</h2><h2 id="三、启动、停止、重启等常用操作"><a href="#三、启动、停止、重启等常用操作" class="headerlink" title="三、启动、停止、重启等常用操作"></a>三、启动、停止、重启等常用操作</h2><p><strong>启动</strong><br>1、直接使用 nginx</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nginx</span><br></pre></td></tr></table></figure>
<p>2、使用 systemctl 命令，这个是 Linux 的服务启动命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl start nginx.service</span><br></pre></td></tr></table></figure>

<p>有没有启动成功呢？可以输入以下命令查看</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ps aux | grep nginx</span><br></pre></td></tr></table></figure>

<p><img src="http://wx4.sinaimg.cn/large/889d1a94gy1fzfeh1ugvwj20nc02baab.jpg" alt="image"></p>
<p><strong>停止</strong><br>1、立即停止服务。这种方法无论进程是否在运行，都直接停止进程。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nginx  -s stop</span><br></pre></td></tr></table></figure>

<p>2、立即停止服务。这种方法相对没那么强硬，需要在进程完成当前工作后再停止。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nginx  -s quit</span><br></pre></td></tr></table></figure>

<p>3、killall 直接杀死进程。如果上面的两个方法无效，可以试试这个。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">killall nginx</span><br></pre></td></tr></table></figure>

<p>4、systemctl 停止。这个和启动方法中第二条类似，都是使用 Linux 的服务命令。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl stop nginx.service</span><br></pre></td></tr></table></figure>

<p><strong>重启</strong><br>重启也是经常会用到的</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl restart nginx.service</span><br></pre></td></tr></table></figure>

<p><strong>重载配置文件</strong><br>有时修改了配置文件可以不用重启，直接重载配置文件就可以了。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nginx -s reload</span><br></pre></td></tr></table></figure>

<p><strong>查看端口号</strong><br>有时候需要查看哪些端口在运行，或者被占用了，可以使用 netstat 命令查看</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">netstat -tlnp</span><br></pre></td></tr></table></figure>

<h2 id="四、自定义错误页"><a href="#四、自定义错误页" class="headerlink" title="四、自定义错误页"></a>四、自定义错误页</h2><p><strong>自定义错误页</strong><br>网站发生错误时一般会返回相应的状态码，比如500、502、503等等，或者404页面。那这些对应的返回页面在 Nginx 里怎么配置呢。</p>
<p>进入 /etc/nginx/conf.d 目录 打开 default.conf 配置文件就可以作相应的修改了</p>
<p><img src="http://ws1.sinaimg.cn/large/889d1a94gy1fzfg3vng9pj20n5027aa3.jpg" alt="image"></p>
<p>添加404页面配置，当然你还要在网站根目录创建一个404.html页面才行</p>
<p><img src="http://wx1.sinaimg.cn/large/889d1a94gy1fzfged7tgjj20nu0i876t.jpg" alt="image"></p>
<p>其中 /404.html 也可以改成一个链接地址如： (<a href="https://pojian.xyz)[https://pojian.xyz]，这样当你访问为404状态时就会直接跳转到你配置的这个链接地址。">https://pojian.xyz)[https://pojian.xyz]，这样当你访问为404状态时就会直接跳转到你配置的这个链接地址。</a></p>
<p>最后还需要你执行重载配置文件或者重启服务器，再访问相应的页面就会看到对应的效果页。</p>
<h2 id="五、访问权限设置"><a href="#五、访问权限设置" class="headerlink" title="五、访问权限设置"></a>五、访问权限设置</h2><p><strong>指令优先级</strong><br>打开配置文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vi &#x2F;etc&#x2F;nginx&#x2F;nginx.conf</span><br></pre></td></tr></table></figure>
<p>在 location 里填写 deny 和 allow 配置信息，deny 和 allow 后面可以填写 ip 或者一个 ip 段，再或者 all，all 表示全部，比如这里我填写的是我本机的ip，allow 表示允许该 ip 的访问，接着重启 nginx</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">nginx -s quit</span><br><span class="line">nginx</span><br></pre></td></tr></table></figure>

<p><img src="http://wx3.sinaimg.cn/large/889d1a94gy1fzgkp66ohwj219e0dugmz.jpg" alt="image"></p>
<p>上面的配置表示只允许 113.118.186.11 进行访问，其他的IP是禁止访问</p>
<p><img src="http://ws2.sinaimg.cn/large/889d1a94gy1fzgl1ra9b1j21jg076jrr.jpg" alt="image"></p>
<p>如果上面的 allow 和 deny 位置互换一下呢？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">deny all;</span><br><span class="line">allow 113.118.186.11;</span><br></pre></td></tr></table></figure>

<p>这里你会发再所有 ip 都不能访问了，这就说明了配置对顺序是有要求的，同一个块下的两个权限指令，先出现的设置会覆盖后出现的设置。</p>
<p><strong>其他访问控制权限匹配</strong><br>有时权限控制会比上面的情况会多样一点，比如站点下的 images 文件是所有用户都可以访问的，而 admin 文件夹只有管理员才能访问，这时就需要 location 来配合实现了</p>
<h2 id="六、配置虚拟主机"><a href="#六、配置虚拟主机" class="headerlink" title="六、配置虚拟主机"></a>六、配置虚拟主机</h2><p><strong>基于端口号</strong><br>基于端口号配置多个虚拟主机，根据不同的端口号来访问不同的站点，原来就是监听端口号。</p>
<p>可以直接配置了主配置文件 etc/nginx/nginx.conf，也可以配在子配置文件目录下 etc/nginx/conf.d，这里拿配置在子文件做示例</p>
<p>etc/nginx/conf.d/default.conf 默认监听的是 <strong>80</strong> 端口，指向的目录是 /data/www</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/nginx/conf.d/default.conf</span><br></pre></td></tr></table></figure>

<p><img src="http://ws3.sinaimg.cn/large/889d1a94gy1fzhod50ihzj21660bct9n.jpg" alt="image"></p>
<p>现在我们配置添加一个新的虚拟主机，监听 <strong>8081</strong> 端口</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/nginx/conf.d/8081.conf</span><br></pre></td></tr></table></figure>

<p>8081.conf 具体配置如下，监听的端口为 <strong>8081</strong>，指向的目录是 /data/www/8081，和 <strong>80</strong> 端口指向的目录不一样</p>
<p><img src="http://wx4.sinaimg.cn/large/889d1a94gy1fzhoi9spuxj216i04wdg7.jpg" alt="image"></p>
<p>这里需要你自己去新建一个 8081 目录和 index.html 文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mkdir /data/www/8081</span><br><span class="line">vim /data/www/8081/index.html</span><br></pre></td></tr></table></figure>

<p>最后访问 xxx.com 和 xxx.com:8081 会访问到不同的站点，当然前提是你的域名已经解析到你的服务器，要不然的话就只能通过 ip 访问了 xxx.xxx.xx.xx 和 xxx.xxx.xx.xx:8081 访问。</p>
<p><strong>基于域名配置</strong></p>
<p>基于域名配置，我这里先说一个前提，就是你的域名已经成功解析到服务器。</p>
<p><img src="http://wx3.sinaimg.cn/large/889d1a94gy1fzhosuczh6j21u2080jsw.jpg" alt="image"></p>
<p>下面开始配置 nginx</p>
<p>/etc/nginx/conf.d/default.conf 配置文件监听的是 <strong>80</strong> 端口，指向目录是 /data/www，注意看 server_name 改成域名地址</p>
<p><img src="http://wx2.sinaimg.cn/large/889d1a94gy1fzhobuxtkxj21660bcdgt.jpg" alt="image"></p>
<p>接着我们来配置另外一个虚拟站点 /etc/nginx/conf.d/8081.conf，注意了 这里监听我端口也是 <strong>80</strong> 端口，不是其他的端口号，但 server_name 和 root 目录跟上面的不一样</p>
<p><img src="http://ws3.sinaimg.cn/large/889d1a94gy1fzhshp7dttj216405ogm3.jpg" alt="image"></p>
<p>这样当你访问 nginx.pojian.xyz 和 nginx2.pojian.xyz 时访问会是配置的不同的两个站点。</p>
<h2 id="七、反向代理"><a href="#七、反向代理" class="headerlink" title="七、反向代理"></a>七、反向代理</h2><p><strong>正向代理</strong><br>A同学在大众创业、万众创新的大时代背景下开启他的创业之路，目前他遇到的最大的一个问题就是启动资金，于是他决定去找马云爸爸借钱，可想而知，最后碰一鼻子灰回来了，情急之下，他想到一个办法，找关系开后门，经过一番消息打探，原来A同学的大学老师王老师是马云的同学，于是A同学找到王老师，托王老师帮忙去马云那借500万过来，当然最后事成了。不过马云并不知道这钱是A同学借的，马云是借给王老师的，最后由王老师转交给A同学。这里的王老师在这个过程中扮演了一个非常关键的角色，就是代理，也可以说是正向代理，王老师代替A同学办这件事，这个过程中，真正借钱的人是谁，马云是不知道的，这点非常关键。</p>
<p>我们常说的代理也就是只正向代理，正向代理的过程，它隐藏了真实的请求客户端，服务端不知道真实的客户端是谁，客户端请求的服务都被代理服务器代替来请求，某些科学上网工具扮演的就是典型的正向代理角色。用浏览器访问 <a target="_blank" rel="noopener" href="http://www.google.com/">http://www.google.com</a>  时，被残忍的block，于是你可以在国外搭建一台代理服务器，让代理帮我去请求google.com，代理把请求返回的相应结构再返回给我。<br><img src="http://wx4.sinaimg.cn/large/889d1a94gy1fzittlk5sgj20zu0kq1kx.jpg" alt="image"></p>
<p><strong>反向代理</strong></p>
<p>大家都有过这样的经历，拨打10086客服电话，可能一个地区的10086客服有几个或者几十个，你永远都不需要关心在电话那头的是哪一个，叫什么，男的，还是女的，漂亮的还是帅气的，你都不关心，你关心的是你的问题能不能得到专业的解答，你只需要拨通了10086的总机号码，电话那头总会有人会回答你，只是有时慢有时快而已。那么这里的10086总机号码就是我们说的反向代理。客户不知道真正提供服务人的是谁。</p>
<p>反向代理隐藏了真实的服务端，当我们请求<a target="_blank" rel="noopener" href="http://www.baidu.com/">www.baidu.com</a> 的时候，就像拨打10086一样，背后可能有成千上万台服务器为我们服务，但具体是哪一台，你不知道，也不需要知道，你只需要知道反向代理服务器是谁就好了，<a target="_blank" rel="noopener" href="http://www.baidu.com/">www.baidu.com</a> 就是我们的反向代理服务器，反向代理服务器会帮我们把请求转发到真实的服务器那里去。Nginx就是性能非常好的反向代理服务器，具体访问哪台服务器Server是由Nginx来控制的，一般用来做负载均衡。</p>
<p><img src="http://ws2.sinaimg.cn/large/889d1a94gy1fzitu7g08oj20sk0nk7pb.jpg" alt="image"></p>
<p>两者的区别在于代理的对象不一样：<strong>正向代理代理的对象是客户端，反向代理代理的对象是服务端</strong></p>
<p><strong>最简单的反向代理</strong></p>
<p>下面就说一个最简单的反向代理配置，先打开我们前面配置的 8081 端口虚拟站点配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/nginx/conf.d/8081.conf</span><br></pre></td></tr></table></figure>

<p><img src="http://ws3.sinaimg.cn/large/889d1a94gy1fzhshp7dttj216405ogm3.jpg" alt="image"></p>
<p>原来的 root 和 index 不要了，换成一个 location 代理地址，看下图</p>
<p><img src="http://wx2.sinaimg.cn/large/889d1a94gy1fziv0a4rtyj21cs05udk2.jpg" alt="image"></p>
<p>这样当你访问 nginx2.pojian.xyz 的时候 nginx 会帮你反向代理到 <a target="_blank" rel="noopener" href="https://pojian.xyz/">https://pojian.xyz</a> 这个地址上去，proxy_pass 一般是填写 ip，但填写域名也可以。</p>
<p><strong>其它一些较常用的反向代理指令</strong></p>
<ul>
<li>proxy_set_header: 在将客户端请求发送给后端服务器之前，更改来自客户端的请求头信息。</li>
<li>proxy_connect_timeout: 配置Nginx与后端代理服务器尝试建立连接的超时时间。</li>
<li>proxy_read_timeout: 配置Nginx向后端服务器组发出read请求后，等待相应的超时时间。</li>
<li>proxy_send_timeout: 配置Nginx向后端服务器组发出write请求后，等待相应的超时时间。</li>
<li>proxy_redirect: 用于修改后端服务器返回的响应头中的Location和Refresh。</li>
</ul>
<h2 id="八、开启-Gzip-压缩"><a href="#八、开启-Gzip-压缩" class="headerlink" title="八、开启 Gzip 压缩"></a>八、开启 Gzip 压缩</h2><p>Nginx提供了专门的gzip模块，并且模块中的指令非常丰富。</p>
<ul>
<li>gzip : 该指令用于开启或 关闭gzip模块。</li>
<li>gzip_buffers : 设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。</li>
<li>gzip_comp_level : gzip压缩比，压缩级别是1-9，1的压缩级别最低，9的压缩级别最高。压缩级别越高压缩率越大，压缩时间越长。</li>
<li>gzip_disable : 可以通过该指令对一些特定的User-Agent不使用压缩功能。</li>
<li>gzip_min_length:设置允许压缩的页面最小字节数，页面字节数从相应消息头的Content-length中进行获取。</li>
<li>gzip_http_version：识别HTTP协议版本，其值可以是1.1.或1.0.</li>
<li>gzip_proxied : 用于设置启用或禁用从代理服务器上收到相应内容gzip压缩。</li>
<li>gzip_vary : 用于在响应消息头中添加Vary：Accept-Encoding,使代理服务器根据请求头中的Accept-Encoding识别是否启用gzip压缩。</li>
</ul>
<p>最简单的配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">http &#123;</span><br><span class="line">    .....</span><br><span class="line">    gzip on;</span><br><span class="line">    gzip_types text/plain application/javascript text/css;</span><br><span class="line">    .....</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="http://wx4.sinaimg.cn/large/889d1a94gy1fzixy8p8c4j21820eogsb.jpg" alt="image"></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">PAN</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://geekweb.site/2019/01/22/nginxbase/">https://geekweb.site/2019/01/22/nginxbase/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://geekweb.site" target="_blank">乘风破浪</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/nginx/">nginx</a><a class="post-meta__tags" href="/tags/centos/">centos</a></div><div class="post_share"><div class="social-share" data-image="https://i.loli.net/2021/03/26/dhQMaZic2KpjLsG.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2019/01/24/wechatnoaccess/"><img class="prev-cover" src="https://i.loli.net/2021/03/26/inMLXSpwIzUmt26.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">非微信官方网页，继续访问将转换成手机预览模式</div></div></a></div><div class="next-post pull-right"><a href="/2019/01/22/macsshconnection/"><img class="next-cover" src="https://i.loli.net/2021/03/26/YT1hgRiNql7V3Hz.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">解决Mac下SSH闲时自动断开的问题</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="https://i.loli.net/2020/09/25/RVKnjUAof6kryGi.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">PAN</div><div class="author-info__description">研究Web前端开发技术和互联网产品</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">13</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">25</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">欢迎光临！</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%89%B9%E7%82%B9"><span class="toc-number">1.</span> <span class="toc-text">特点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E5%AE%89%E8%A3%85"><span class="toc-number">2.</span> <span class="toc-text">一、安装</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81Nginx-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E8%A7%A3%E6%9E%90"><span class="toc-number">3.</span> <span class="toc-text">二、Nginx 配置文件解析</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E5%90%AF%E5%8A%A8%E3%80%81%E5%81%9C%E6%AD%A2%E3%80%81%E9%87%8D%E5%90%AF%E7%AD%89%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C"><span class="toc-number">4.</span> <span class="toc-text">三、启动、停止、重启等常用操作</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E8%87%AA%E5%AE%9A%E4%B9%89%E9%94%99%E8%AF%AF%E9%A1%B5"><span class="toc-number">5.</span> <span class="toc-text">四、自定义错误页</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E8%AE%BF%E9%97%AE%E6%9D%83%E9%99%90%E8%AE%BE%E7%BD%AE"><span class="toc-number">6.</span> <span class="toc-text">五、访问权限设置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AD%E3%80%81%E9%85%8D%E7%BD%AE%E8%99%9A%E6%8B%9F%E4%B8%BB%E6%9C%BA"><span class="toc-number">7.</span> <span class="toc-text">六、配置虚拟主机</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%83%E3%80%81%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86"><span class="toc-number">8.</span> <span class="toc-text">七、反向代理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AB%E3%80%81%E5%BC%80%E5%90%AF-Gzip-%E5%8E%8B%E7%BC%A9"><span class="toc-number">9.</span> <span class="toc-text">八、开启 Gzip 压缩</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/11/25/http%E5%9B%BE%E8%A7%A3/" title="http图解"><img src="https://i.loli.net/2021/03/26/YT1hgRiNql7V3Hz.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="http图解"/></a><div class="content"><a class="title" href="/2022/11/25/http%E5%9B%BE%E8%A7%A3/" title="http图解">http图解</a><time datetime="2022-11-25T02:46:58.000Z" title="发表于 2022-11-25 10:46:58">2022-11-25</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2019/04/20/%E5%A6%82%E4%BD%95%E4%BF%9D%E8%AF%81%E9%A1%B5%E9%9D%A2%E6%96%87%E4%BB%B6%E8%83%BD%E8%A2%AB%E5%AE%8C%E6%95%B4%E9%80%81%E8%BE%BE%E6%B5%8F%E8%A7%88%E5%99%A8/" title="如何保证页面文件能被完整送达浏览器？"><img src="https://i.loli.net/2021/03/26/dhQMaZic2KpjLsG.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="如何保证页面文件能被完整送达浏览器？"/></a><div class="content"><a class="title" href="/2019/04/20/%E5%A6%82%E4%BD%95%E4%BF%9D%E8%AF%81%E9%A1%B5%E9%9D%A2%E6%96%87%E4%BB%B6%E8%83%BD%E8%A2%AB%E5%AE%8C%E6%95%B4%E9%80%81%E8%BE%BE%E6%B5%8F%E8%A7%88%E5%99%A8/" title="如何保证页面文件能被完整送达浏览器？">如何保证页面文件能被完整送达浏览器？</a><time datetime="2019-04-20T04:23:08.000Z" title="发表于 2019-04-20 12:23:08">2019-04-20</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2019/04/15/jscopy/" title="如何实现一个深浅拷贝"><img src="https://i.loli.net/2021/03/26/JmzkRwbiO7G1Tqx.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="如何实现一个深浅拷贝"/></a><div class="content"><a class="title" href="/2019/04/15/jscopy/" title="如何实现一个深浅拷贝">如何实现一个深浅拷贝</a><time datetime="2019-04-15T13:11:06.000Z" title="发表于 2019-04-15 21:11:06">2019-04-15</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2019/04/07/jsprototype/" title="JS原型和原型链"><img src="https://i.loli.net/2021/03/26/cBT8YwElLCU5qFV.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS原型和原型链"/></a><div class="content"><a class="title" href="/2019/04/07/jsprototype/" title="JS原型和原型链">JS原型和原型链</a><time datetime="2019-04-07T04:30:08.000Z" title="发表于 2019-04-07 12:30:08">2019-04-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2019/04/02/jsdata/" title="JS的数据类型"><img src="https://i.loli.net/2021/03/26/cBT8YwElLCU5qFV.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS的数据类型"/></a><div class="content"><a class="title" href="/2019/04/02/jsdata/" title="JS的数据类型">JS的数据类型</a><time datetime="2019-04-02T03:22:00.000Z" title="发表于 2019-04-02 11:22:00">2019-04-02</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2018 - 2023 By PAN</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><div class="js-pjax"></div><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-show-text.min.js" data-mobile="false" data-text="I,LOVE,YOU,富强,民主,文明,和谐,平等,公正,法治,爱国,敬业,诚信,友善" data-fontsize="15px" data-random="false" async="async"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>